﻿
@{
    ViewBag.Title = "List";
    Layout = "~/Views/Shared/_Layout.cshtml";

}

<section class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1>商品分类展示</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="#">首页</a></li>
                    <li class="breadcrumb-item active">商品分类</li>
                </ol>
            </div>
        </div>
    </div><!-- /.container-fluid -->
</section>

<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="card card-info">

                <a href="/ProductCategory/Add" class="btn btn-primary">添加</a>


                <div class="card-body">
                    <div class="row">
                        <div class="col-sm-12">
                            <table id="table" class="table table-bordered table-hover dataTable" role="grid" aria-describedby="example2_info " style="width: 100%;">
                                <thead>
                                    <tr role="row">
                                        <th></th>
                                        <th>序号</th>
                                        <th>分类名称</th>
                                        <th>排序号</th>
                                        <th>分类图标</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>

                            </table>
                        </div>
                    </div>
                </div>
                <!-- /.card-body -->

            </div>
            <!-- /.card -->
        </div>
    </div>
</section>
@section js{
    <script>
        function category(){
            alert(1)
            $("#category").addClass("active");
            $("#product").removeClass("active");
            $("#brand").removeClass("active");
        }
        function update(id) {
            window.location.href = "/ProductCategory/Update?id=" + id + "";
        }
        function del(id) {
            //var code = $(".del").attr("code");
            //alert(code);

            var formData = new FormData();
            formData.append("id", id)
            //alert(id);
            var r = confirm("是否删除")
            if (r == true) {
                $.ajax({
                    url: "/ProductCategory/Del",
                    type: "POST",
                    dateType: "json",
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function (result) {
                        if (result.state) {
                            toastr.success('删除成功')
                            $("#table").DataTable().ajax.reload();
                        } else {
                            toastr.error(result.msg)

                        }
                    },
                    error: function (result) {
                        toastr.error('删除失败')
                    }
                })
            }
            else {
                toastr.success("您取消了操作");
            }
        }
       
        $(function () {
            //侯海啸
            $("#table").DataTable({
                "lengthchange": false,
                "searching": false,
                "ordering": false,
                "info": false,
                "paging": false,
                "autoWidth": true,
                "processing": true,
                "language": {
                    "processing": "请求数据中...",
                    "zeroRecords": "没有数据"
                },
                "treeGrid": {
                    "left": 15,
                    "expandIcon": '<span><i class="fa fa-plus-square"></i></span>',
                    "collapseIcon": '<span><i class="fa fa-minus-square"></i></span>'
                },
                "serverSide": true,
                "ajax": function (data, callback, settings) {
                    var param = {};
                    param.draw = data.draw;
                    $.ajax({
                        type: "POST",
                        url: "../ProductCategory/GetAll",
                        cache: false,
                        data: param,
                        dataType: "json",
                        success: function (result) {
                            setTimeout(function () {
                                callback(result);
                            }, 500);
                        }

                    });
                },
                columns: [
                    {
                        className: 'treegrid-control',
                        data: function (data) {
                            if (data.children.length > 0) {
                                return '<span><i class="fa fa-plus-square"></i></span>';
                            }
                            return '';
                        }
                    },
                    { "data": "ID" },
                    { "data": "Name" },
                    { "data": "OrderNum" },
                    {
                        "data": function (data) {

                            var html = "<img src=\"/CategoryImages/" + data.Img + "\" width=\"50\" height=\"50\"></img>";

                            return html;
                        }
                    },
                    {
                        "data": function (data) {

                            var html = "<button  onclick=\"update(" + data.ID + ");\" class = \"btn btn-sm btn-primary\">编辑</button>";

                            if (data.children.length == 0) {
                                html += "&nbsp;&nbsp;";

                                html += "<a href=\"/ProductAttrKey/List?id="+data.ID+"\" style=\"color: #fff;\" class = \"btn btn-sm btn-primary\">属性设置</a>";
                                html += "&nbsp;&nbsp;";
                                html += "<a href=\"/ProductAttrKeySku/List?id=" + data.ID +"\" style=\"color: #fff;\" class = \"btn btn-sm btn-primary\">规格设置</a>";

                            }
                            html += "&nbsp;&nbsp;";
                            html += "<button type =\"button\" onclick=\"del(" + data.ID + ");\"  class = \"del btn btn-sm btn-danger\">删除</button>";
                            return html;
                        }
                    },

                ]
            });

            //$('#table').Datatable({
            //    "paging": true,
            //    "lengthchange": false,
            //    "searching": false,
            //    "ordering": true,
            //    "info": false,
            //    "autowidth": false,
            //});
        });
    </script>
}
